/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.task;

import cn.hutool.core.collection.CollectionUtil;
import com.medusa.gruul.integral.api.entity.IntegralLogIn;
import com.medusa.gruul.integral.service.IIntegralEnterService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 每日23:59修改用户登入状态
 * @Author: xiaoq
 * @Date : 2019/12/31 13:45
 */
@JobHandler(value = "enterUpdateJob")
@Component
@Slf4j
public class EnterUpdateJob extends IJobHandler {

	@Autowired
	private IIntegralEnterService integralEnterService;

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public ReturnT<String> execute(String s) throws Exception {
		log.info("----------- 执行修改登入状态为false的用户登入天数为1L-------------");
		List<IntegralLogIn> integralLogIns = integralEnterService.selectStatusAll();
		if (CollectionUtil.isNotEmpty(integralLogIns)) {
			integralLogIns.stream().forEach(integralLogIn -> {
				integralLogIn.setRunningDays(1L);
				try {
					integralEnterService.updateEnterDays(integralLogIn);
				} catch (Exception e) {
					log.error("需要更新的id".concat(integralLogIn.toString()) + e.getMessage(), e);
				}
			});
		}
		log.info("----------- 执行修改登入状态为false的用户登入天数为1L定时任务执行结束-------------");
		log.info("----------- 执行修改所有用户的登入信息为false-------------");
		List<IntegralLogIn> list = integralEnterService.selectAll();
		if (CollectionUtil.isNotEmpty(list)) {
			list.stream().forEach(integralLogIn -> {
				integralLogIn.setLogInState(false);
				try {
					integralEnterService.updateEnterStatus(integralLogIn);
				} catch (Exception e) {
					log.error("登入id:" + integralLogIn.toString() + e.getMessage(), e);
				}
			});
		}
		log.info("----------- 执行修改所有用户的登入信息为false 定时任务执行结束-------------");
		return SUCCESS;
	}
}

